/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlunit.code;

import java.net.URLDecoder;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @author TiTa
 */
public class queue_url {
    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




/**
 *
 * @author TiTa
 */

    
    
    Queue<String> queue=new LinkedList<String>();
    
    public boolean dungsai;
    public String noidung_url;
    
    
     final Lock lock = new ReentrantLock();
final Condition condition = lock.newCondition();



    public   String lay()
    {
        String ketwa;
        lock.lock();
        try{
        while(dungsai==false){
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        dungsai=false;  
         ketwa=noidung_url;
     System.out.println("\t lay " + this.noidung_url);
       //  notify();
       
        
         queue.poll();
         condition.signalAll();
        }finally{
            lock.unlock();
        }
        return ketwa;   
    }
    
    
    
    
    public void tra(String url) 
    {
        lock.lock();
        try{
            
         while(dungsai==true){
            try {
                condition.await();
            } catch (InterruptedException e) {
                
            }
        }
    this.noidung_url = url;
    dungsai=true;
    //print onto screen
    queue.offer(url);
  System.out.println(" tra " + queue.poll());  //System.out.println(" tra " + this.noidung_url);
    
     //notify();
     condition.signalAll();
     
       }finally{
       lock.unlock();
        }
}
    
    
    
   
    
    boolean rong(){
        
        return ((queue.isEmpty()) ? true:false);
    }


}
